package tech.abing.spider.task.pageprocessor;

import java.util.List;
import java.util.regex.Pattern;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.Five8Entity;
import tech.abing.spider.util.JsoupUtil;
import tech.abing.spider.util.StringUtils;

public class Five8Processor2 implements PageProcessor {
	static Pattern normalContain = Pattern.compile("58");
	
	@Override
	public PageScript getJavaScript() {
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		return normalContain;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
	
		Document doc = Jsoup.parse(page.getContent());
		
		Five8Entity five8 = new Five8Entity();
		
		five8.setUrl(page.getRequest().getUrl());
		five8.setCategory(StringUtils.toString(page.getRequest().getParamsByName("category")));
		
		five8.setPosition(doc.select("div[class=\"leftbar pad60\"] > h1").text());
		
		Elements compEles = doc.select("div.compMsg > a");
		if(compEles != null && compEles.size() == 2){
			five8.setCompanyName(compEles.first().text());
			five8.setCertification(compEles.last().attr("title"));
		}
		
		five8.setPubDate(doc.select("span.timeD").text());
		
		
		
		//entityId=23082862059168&entityType=0&psid=136752151188856240802325487
		String roiAdd = doc.select("#roi_add").val();
		String[] roiArr = roiAdd.split("&");
		String infoid = roiArr[0].substring(roiArr[0].indexOf("=") + 1);
		
		//http://jst1.58.com/counter?infoid=23082862059168
		String counter = JsoupUtil.fetchByJsoup("http://jst1.58.com/counter?infoid="+infoid);
		five8.setReview(counter.substring(counter.lastIndexOf("=") + 1));
		
		//five8.setReview(doc.select("#totalcount").text());
		
		//http://statistics.zp.58.com/position/totalcount/?infoId=23082862059168&userId=19573653332230
		String totalcount = JsoupUtil.fetchByJsoup("http://statistics.zp.58.com/position/totalcount/?infoId="+infoid);
		
		totalcount = totalcount.substring(totalcount.indexOf("(") + 1, totalcount.indexOf(")"));
		JSONObject json = JSONObject.fromObject(totalcount);
		five8.setPush(json.getString("deliveryCount"));
		
		//five8.setPush(doc.select("#deliveryCount").text());
		
		Element addEle = doc.select("div.addMsg").first();
		if(addEle != null){
			five8.setRegister(addEle.text().split("：")[1]);
		}
		
		try {
			Element ele = doc.select("div.xq").first();
			five8.setNeedNum(ele.select("dl:eq(1) > dd").text());
			five8.setValidDate(ele.select("dl:eq(2) > dd").text());
			five8.setSalary(ele.select("dl:eq(3) > dd").text());
			five8.setWorkAdr(ele.select("dl:eq(5) > dd").text());
		} catch (Exception e) {
			System.out.println("被封了！！！");
			e.printStackTrace();
		}
		
		five8.setPosDesc(doc.select("#zhiwei > dl > dd").text());
		five8.setCompDesc(doc.select("#gongsi > p").text());
		
		String linkMan = doc.select("#lianxi > dl:eq(0) > dd").first().text();
		linkMan = linkMan.substring(0, linkMan.indexOf("(")).trim();
		five8.setLinkman(linkMan);
		
		Element telEle = doc.select("#showphone > img").first();

		if(telEle != null){
			String telUrl = telEle.attr("src");
			
			String pagenum = doc.select("#pagenum").val();
			
			String [] pns = pagenum.split("_");
			
			String preTelUrl = telUrl.substring(0, telUrl.indexOf("&v=") + 3);
			
			five8.setPhone(preTelUrl + pns[0]);
			
			if(pns.length > 1){
				
				five8.setPhone2(preTelUrl + pns[1]);
			}
			
		}
		
		Element lianxiEle = doc.select("#lianxi > dl:eq(3) > dd").first();
		if(lianxiEle != null){
			five8.setCompAdr(lianxiEle.text());
		}
		
		objectContainer.add(five8);
		
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
